package com.gitee.xmhzzz.component.shutdown.graceful.hook;

import com.gitee.xmhzzz.component.shutdown.graceful.config.ShutdownConfig;
import com.gitee.xmhzzz.component.shutdown.graceful.event.ApplicationShutdownEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;

/**
 * @ClassName HookShutdown
 * @Description
 * @Author wzq
 * @Date 2023/11/6 10:48
 * @Version 1.0
 */
@Slf4j
@EnableConfigurationProperties(ShutdownConfig.class)
@ConditionalOnProperty(value = "xmhzzz.graceful.shutdown.enable", havingValue = "true")
public class HookShutdown {

    public HookShutdown(ApplicationContext applicationContext) {
        log.info("HookShutdown init..........");
        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            applicationContext.publishEvent(new ApplicationShutdownEvent(applicationContext));
        }));
    }
}
